package com.hunliji.monitor.web.filter;

import com.alibaba.fastjson.JSON;
import com.hunliji.monitor.service.api.model.bo.UserInfoBO;
import com.hunliji.monitor.share.model.AdminBO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class GeneralContext {
    private static final Logger logger = LoggerFactory.getLogger(GeneralContext.class);

    private static final String MEMBER_INFO_KEY = "admin_key";
    private static final ThreadLocal<Map<String, Object>> context = new ThreadLocal<>();

    /**
     * 获取用户信息
     * @return
     */
    public static UserInfoBO getAdmin(){
        Map<String,Object> map = context.get();
        logger.info("当前threadLocal值:{}", JSON.toJSONString(context));
        logger.info("当前threadLocal取出的值:{}", JSON.toJSONString(map));
        if(Objects.isNull(map)){
            return null;
        }else {
            Object obj = map.get(MEMBER_INFO_KEY);
            if(Objects.isNull(obj)){
                return null;
            }
            return (UserInfoBO) obj;
        }
    }

    /**
     * 设置用户信息
     */
    public static void setAdmin(UserInfoBO userInfoBO){
        Map<String,Object> map = context.get();
        if(Objects.isNull(map)){
            map = new ConcurrentHashMap<>(16);
        }
        map.put(MEMBER_INFO_KEY,userInfoBO);
        context.set(map);
    }

}
